/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.sessionbean;

import cms.dataaccess.DAOFactory;
import dto.Profile;
import dto.User;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author TauPo
 */
@Stateless(mappedName="UserPortfoilo")
public class UserPortfoilo implements UserPortfoiloRemote {

    @Override
    public boolean saveUser(User usrDTO) {
      try{
      DAOFactory.getInstance().getUserDAO().insertUser(usrDTO);
      return true;
      }
      catch(Exception e ){
          System.err.println("creating user fail:"+e.getMessage());
          return false;
      }
    }

    @Override
    public boolean deleteUser(String userName) {
        try{
            User user = this.getUser(userName);
            if(user ==null){
                return false;
            }
            DAOFactory.getInstance().getUserDAO().deleteUser(user);
            return true;
        }catch(Exception e){
            System.err.println(" deleting user fail by user name:"+userName+"fail ==>"+e.getMessage());
        }
        return false;
       
    }

    @Override
    public boolean updateUser(User usrDTO) {
       if(usrDTO==null){
           return false;
       }
      try{
        DAOFactory.getInstance().getUserDAO().updateUser(usrDTO);
        return true;
      }
      catch(Exception e ){
          System.err.println("updating user fail for use name"+usrDTO.getUserName()+"==>"+e.getMessage());
          return false;
      }
    }

    @Override
    public List<User> getAllUsers(String role) {
        try{
        return DAOFactory.getInstance().getUserDAO().findByCustomizeQuery("role='"+role+"'");
        
      }
      catch(Exception e ){
          System.err.println("getting all  user fail by rol:"+role+"==>"+e.getMessage());
          return null;
      }
    }

    @Override
    public List<User> getAllUsers() {
     try{
        return DAOFactory.getInstance().getUserDAO().findAllUser();
        
      }
      catch(Exception e ){
          System.err.println("getting all user fail==>"+e.getMessage());
         return null;
      }
    }

    @Override
    public Profile getUserProfile(String userName) {
        try{
            return DAOFactory.getInstance().getProfileDAO().findProfile(userName);
         
        }catch (Exception e){
            System.err.println("getting user profile fail for user name "+ userName + "==>"+e.getMessage());
        }
        return null;
    }

    @Override
    public boolean userLogin(String userName, String password, String role) {
        try{
         return DAOFactory.getInstance().getUserDAO().Login(userName, password,role); 
        }catch(Exception e )
        {
            System.err.println("login fail for user id"+userName+ "==>" + e.getMessage());
            return false;
        }
  
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public User getUser(String userName) {
       try{
          return  DAOFactory.getInstance().getUserDAO().findUser(userName);
      }
      catch(Exception e ){
          System.err.println("getting  user by name:"+userName + " fail ==> "+e.getMessage());
          return null; 
      }
    }

    @Override
    public boolean updateUserProfile(Profile userProfile) {
        if(userProfile==null){
            return false;
        }
         try{
           DAOFactory.getInstance().getProfileDAO().updateProfile(userProfile);
           return true;
      }
      catch(Exception e ){
          System.err.println("updateUserProfile fail:"+userProfile.getUserName() + " fail ==> "+e.getMessage());
          return true; 
      }
    }

}
